SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE VIEW dbo.vw_PlazasAfectadasF
AS
SELECT ss.idsubservicio, p.cupof  AS CUPOF, 
    CASE f.regimensalarial WHEN 12 THEN NULL 
    ELSE SUM(DISTINCT sa2.horas) END AS HORAS
FROM tb_serviciosagentes sa1, tb_funciones f, 
    tb_serviciosagentes sa2, tb_plazas p, tb_plazas p2, 
    tb_subserviciosagentes ss, 
    tb_subserviciosagentes ss2
WHERE sa1.idservicioagente = ss.servicioagente AND 
    sa2.agente = sa1.agente AND p.idplaza = ss2.plaza AND 
    ss.normafin = ss2.normafin AND 
    ss2.servicioagente = sa2.idservicioagente AND 
    p.funcion = f.idfuncion 
GROUP BY p.cupof , ss.idsubservicio, f.regimensalarial





GO
GRANT SELECT ON  [dbo].[vw_PlazasAfectadasF] TO [SoloVer]
GO
